package com.kawa.kawa_umeng;

import android.app.Activity;
import android.util.Log;

import androidx.annotation.NonNull;

import com.umeng.analytics.MobclickAgent;
import com.umeng.commonsdk.UMConfigure;
import com.umeng.commonsdk.debug.UMLogUtils;
import com.umeng.commonsdk.listener.OnGetOaidListener;

import java.util.HashMap;
import java.util.Map;
import java.util.logging.Handler;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry.Registrar;

/** KawaUmengPlugin */
public class KawaUmengPlugin implements FlutterPlugin, MethodCallHandler, ActivityAware {
  private Activity mActivity;
  private MethodChannel channel;

  @Override
  public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
    channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "kawa_umeng");
    channel.setMethodCallHandler(this);
  }

  @Override
  public void onMethodCall(@NonNull MethodCall call, @NonNull final Result result) {
    Map<String,String> params = new HashMap<>();
    if (call.arguments != null){
      params = (Map<String, String>) call.arguments;
    }
    switch (call.method){
      case "init":
        UMConfigure.init(mActivity,params.get("appkey"),params.get("channel"),UMConfigure.DEVICE_TYPE_PHONE,null);
        result.success(true);
        break;
      case "openDebug":
        UMConfigure.setLogEnabled(params.get("isDebug").equals("1"));
        result.success(true);
        break;
      case "oaid":
        UMConfigure.getOaid(mActivity, new OnGetOaidListener() {
          @Override
          public void onGetOaid(final String s) {
            mActivity.runOnUiThread(new Runnable() {
              @Override
              public void run() {
                result.success(s);
              }
            });
          }
        });
        break;
      case "pageStart":
        MobclickAgent.onPageStart(params.get("page"));
        result.success(true);
        break;
      case "pageEnd":
        MobclickAgent.onPageEnd(params.get("page"));
        result.success(true);
        break;
      case "event":
        MobclickAgent.onEvent(mActivity,params.get("key"));
        result.success(true);
        break;
      case "eventCount":
        String key = params.get("key");
        params.remove("key");
        Log.e(key,params.toString());
        MobclickAgent.onEvent(mActivity,key,params);
        result.success(true);
        break;
      default:
        result.success(true);
        break;
    }
  }

  @Override
  public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
    channel.setMethodCallHandler(null);
  }

  @Override
  public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) {
    mActivity = binding.getActivity();
  }

  @Override
  public void onDetachedFromActivityForConfigChanges() {

  }

  @Override
  public void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBinding binding) {

  }

  @Override
  public void onDetachedFromActivity() {

  }
}
